package cqu.zhaiyixin20215521.huarondao.slice.pictureSelect;

import ohos.agp.components.ListContainer;
import ohos.agp.image.Effect;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Texture;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.agp.window.dialog.ToastDialog;
import ohos.media.image.PixelMap;
import ohos.agp.image.ImageEffect;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;
import ohos.sysappcomponents.settings.SystemSettings;
import org.jetbrains.annotations.NotNull;

//图片处理器
public class PicProcessor {
    //图片按比例裁剪
    public static PixelMap crop(@NotNull PixelMap input, double height_width_ratio) {
        PixelMap output;
        Rect rect = new Rect();
        if(input.getImageInfo().size.height / (double)input.getImageInfo().size.width < height_width_ratio) {
            //太宽
            rect.height = input.getImageInfo().size.height;
            rect.width = (int) (rect.height / height_width_ratio);
            rect.minY = 0;
            rect.minX = (input.getImageInfo().size.width - rect.width) / 2;
        } else {
            //太高
            rect.width = input.getImageInfo().size.width;
            rect.height = (int) (rect.width * height_width_ratio);
            rect.minX = 0;
            rect.minY = (input.getImageInfo().size.height - rect.height) / 2;
        }
        Size size = new Size(rect.width, rect.height);
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.size = size;
        output = PixelMap.create(input, rect, options);
        return output;
    }

    //图片切块
    public static PixelMap[] cutUp(PixelMap input, int rowNum, int colNum) {
        PixelMap[] outputs = new PixelMap[rowNum * colNum];
        Rect rect = new Rect();
        rect.height = input.getImageInfo().size.height / rowNum;
        rect.width = input.getImageInfo().size.width / colNum;
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.size = new Size(rect.width, rect.height);
        for(int i = 0; i < rowNum; ++i) {
            for(int j = 0; j < colNum; ++j) {
                rect.minY = i * rect.height;
                rect.minX = j * rect.width;
                PixelMap pixelMap = PixelMap.create(input, rect, options);
                outputs[i * colNum + j] = pixelMap;
            }
        }
        return outputs;
    }

    //图片上添加文字
    public static PixelMap drawText(PixelMap input, String text) {
        PixelMap output = PixelMap.create(input, null);
        Size pictureSize = output.getImageInfo().size;
        Canvas canvas = new Canvas(new Texture(output));
        Paint paint = new Paint();
        paint.setColor(Color.GRAY);
        paint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
        paint.setTextSize(pictureSize.width / 3);
        canvas.drawText(paint, text, pictureSize.width / 10, pictureSize.height * 9 / 10);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setTextSize(pictureSize.width / 3);
        paint.setStrokeWidth(paint.getTextSize() / 20);
        canvas.drawText(paint, text, pictureSize.width / 10, pictureSize.height * 9 / 10);
        return output;
    }

    public static PixelMap drawBorder(PixelMap input) {
        PixelMap output = PixelMap.create(input, null);
        Size pictureSize = output.getImageInfo().size;
        Canvas canvas = new Canvas(new Texture(output));
        Paint paint = new Paint();
        paint.setColor(Color.DKGRAY);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(pictureSize.width /(float) 60);
        RectFloat rect = new RectFloat(0, 0, pictureSize.width, pictureSize.height);
        canvas.drawRoundRect(rect, 10F, 10F, paint);
        return output;
    }
}
